package problem165;

//比较版本号
//https://leetcode.cn/problems/compare-version-numbers/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution {
    public int compareVersion(String version1, String version2) {
        String[] v1 = split(version1);
        String[] v2 = split(version2);
        System.out.println(Arrays.toString(v1));
        System.out.println(Arrays.toString(v2));
        int m = Math.max(v1.length, v2.length);
        for(int i = 0; i<m; i++) {
            int a = i < v1.length ? Integer.valueOf(v1[i]) : 0;
            int b = i < v2.length ? Integer.valueOf(v2[i]) : 0;
            System.out.println(a + " " + b);
            if(a < b) {
                return -1;
            }else if(a > b) {
                return 1;
            }
        }
        return 0;
    }

    private String[] split(String s) {
        int p = 0, n = s.length();
        List<String> list = new ArrayList<>();
        for(int i = 0; i<n; i++) {
            if(s.charAt(i) == '.') {
                list.add(s.substring(p, i));
                p = i + 1;
            }
        }
        list.add(s.substring(p));
        return list.toArray(new String[list.size()]);
    }
}